As a JavaScript beginner, it is essential to know the basics of the language. This concept may come in handy for day to day programming in JavaScript
Note: In this article, we will go through some of the key concepts in JavaScript that can be used as a reference for building JavaScript applications.
There are three possible keywords to define the variable. We have var since the beginning of the JavaScript. We also have let and const which were introduced in ES2015. After the update to the modern JavaScript, we get fully supported let and const in the browser.
Rules and Conventions for variables:
Cannot start with a number
Can include letters, numbers, underscore(_), a dollar sign ($)
Can start a variable with a $ sign, but not with numbers, but assigning with a $ sign is not recommended, until and unless you are using jquery
Can have CamelCasing, underscore
Let’s quickly look at the examples and usage:
Initialize the variable and assign the variable with some value
Assign the value to the variable without initializing the variable
Variable with CamelCasing and underscore
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// var, let, const //usage of var //Initialize the variable and assign the variable with some value var companyname; companyname = 'Topcoder'; console.log(companyname); //reassign it with different value companyname = 'xyz'; console.log(companyname); //Assign the value to the variable without initializing the variable companyname = 'Topcoder'; console.log(companyname); //Variable with Camel casing and underscore companyname = 'Topcoder'; console.log(companyname); company_name = 'Topcoder'; console.log(company_name);
Output
1 2 3 4 5
Topcoder xyz Topcoder Topcoder Topcoder
let
Let works exactly the same way as a variable. You can initialize, create the variable, assign the value, and reassign the value
Example:
1 2 3 4 5 6 7 8 9
//usage of let //initialize the variable let companyname; companyname = 'Topcoder'; console.log(companyname); //re-assign the variable with a different value companyname = 'xyz'; console.log(companyname);
Output
1 2
Topcoder xyz
Please try other examples with let in a similar way as var. You can refer to the usage of var and implement in a similar way with let for all other use cases
Const
Const works in a different way and it stands for constant. It means we cannot change the values or values cannot be reassigned
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//initialize the variable
const companyname = 'Topcoder';
companyname = 'xyz';
console.log(companyname);
Output:
Uncaught TypeError: Assignment to constant variable.
This means you cannot re - assign the
const variable with another value
const person = {
name: 'john',
age: 50
}
person.name = 'jay'
console.log(person); // This change the person name to jay ,but the object still remains the same
Output: {
name: "jay",
age: 50
}
//let us consider the array of numbers
const numbers = [1, 2, 3, 4, 5, 6]
console.log(numbers);
numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(numbers);
Output:
Uncaught TypeError: Assignment to constant variable.
(Again you cannot assign the
const variable with different array)
Primitive Data Type
They are stored on the stack, directly in the location the variable access.
JavaScript has 6 primitive data types:
String
Number
Boolean
Null
Undefined
Symbols(ES6)
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//string
const companyname = 'Topcoder' //number
const age = 26 //boolean
const isSingle = true;
//null
const aircraft = null;
//undefined
let firstname;
//symbol
const sym = Symbol()
console.log(typeof companyname)
console.log(typeof age)
console.log(typeof isSingle)
console.log(typeof aircraft)
console.log(typeof firstname)
console.log(typeof sym)
Output:
1 2 3 4 5 6
string / number / boolean object undefined symbol
Reference Data Types:
It is a pointer to the location in memory and is accessed by reference and not by value. Objects are stored on a heap, which will be dynamic in nature.
Reference data types / Objects:
rays
Object Literals
Functions
Dates
Reference types return as objects.
Example:
1
2
3
4
5
6
7
8
9
//reference data types
const person = {
name: 'jay'
}
console.log(typeof person)
//expected output: Object
const numbers = [1, 2, 3, 4]
console.log(typeof numbers)
//expected output: Object
Most other languages like C# and Java are statically typed languages. JavaScript is a dynamically typed language, it can hold multiple data types and does not need to define any types. It can hold the string and a number for the same variable in the code without getting any issues or errors.
There are technologies which can turn JavaScript to statically typed language, like TypeScript.
You may face some cases, where you will need to convert the variable type from string to integer, or integer to string, or boolean to string.
Examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Type Conversion
//bool to String
let released = true;
released = String(true);
console.log(released); // true
console.log(released.length); //4
//using toString()
released = (true)
.toString()
console.log(released); //true
console.log(released.length); //4
//number to string
val = String(1234)
console.log(val); //1234
console.log(val.length); //4
//string to number
num = Number('8')
console.log(num); //8
//bool to number
num = Number(true)
console.log(num); //1
num = Number(false)
console.log(num); //0
//addition example - In this example, num2 is converted to the string
// while addition operation and concatenation is performed
num1 = String(1)
num2 = 7
add = num1 + num2;
console.log(add); // 17
console.log(typeof add); //string
In this article, we saw some of the essential concepts of JavaScript which are very important for any developer to begin with the JavaScript language. I hope you now have a good understanding of these important concepts.
In the next article, we shall discuss more concepts of JavaScript, until then feel free to explore the world of JavaScript.